package com.bjpowernode.service.impl;

import cn.hutool.core.util.IdUtil;
import com.bjpowernode.constant.Constant;
import com.bjpowernode.entity.TTran;
import com.bjpowernode.mapper.TTranMapper;
import com.bjpowernode.query.BaseQuery;
import com.bjpowernode.query.TranQuery;
import com.bjpowernode.service.TranService;
import com.bjpowernode.util.LoginInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class TranServiceImpl implements TranService {

    @Resource
    private TTranMapper tTranMapper;

    @Override
    public int createTran(TranQuery tranQuery) {
        TTran tTran = new TTran();
        BeanUtils.copyProperties(tranQuery, tTran);

        //交易流水号
        /*
            生成全局唯一的值：
                UUID 缺点 长  没有规律
                雪花算法 优点 短  有规律  时间（时钟回拨）  机房编号  机器编号  自增值
         */
        long snowflakeNextId = IdUtil.getSnowflakeNextId();
        tTran.setTranNo(String.valueOf(snowflakeNextId));

        tTran.setCreateTime(new Date());
        tTran.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());

        return tTranMapper.insertSelective(tTran);
    }

    @Override
    public PageInfo<TTran> getTranByPage(Integer current) {
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        List<TTran> list = tTranMapper.selectByPage(new BaseQuery());

        PageInfo<TTran> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public TTran getTranById(Integer id) {
        return tTranMapper.selectById(id);
    }
}
